home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
DEBUG
/
OBJTEST
/
OBJUNIT.PAS
< prev
Wrap
Pascal/Delphi Source File
|
1996-06-06
|
4KB
|
124 lines
unit Objunit;
interface
uses classes, stdctrls, sysutils;
type
{Its usually a good idea to start from TComponent if methods are involved.
True? Why?}
TMyObjectClass = class(TComponent)
private
FNeedToCalc: boolean; {True = Mean needs to be recalculated}
FOnDataChange: TNotifyEvent; {The memo contents changed}
FMean: double; {The current value of the mean}
function GetMean: double;
public
{Generally, all data should be private and access methods provided.
However, in this case if one wanted the user to have complete control
over the memo, it might be OK.}
FMemo: TMemo;
constructor create(Owner: TComponent); override;
destructor Destroy; override;
procedure MyChange(Senter: TObject);
property Mean: double read GetMean;
property OnDataChange: TNotifyEvent read FOnDataChange write FOnDataChange;
{Making this function public allows access by the application program.
See Comments on Calc button.}
function CalcMean(Data: TStringList): double;
end;
implementation
uses main;
{''----------------------------------------------------------------------------}
constructor TMyObjectClass.Create(Owner: TComponent);
begin
inherited Create(Owner);
FMemo := TMemo.Create(Self);
FMemo.TabStop := false;
{1. When the Memo's OnChange event fires, it causes the procedure MyChange to
execute.}
FMemo.OnChange := MyChange;
end;
{''----------------------------------------------------------------------------}
procedure TMyObjectClass.MyChange(Senter: TObject);
begin
{The contents of the memo has changed, so need to recalculate the mean}
FNeedToCalc := true;
{2. If FOnDataChange points to a procedure, then its executed. In this case,
when the object is created, it is set equal to TfrmMain.OnDataChange}
if Assigned(FOnDataChange) then FOnDataChange(Self);
end;
{''----------------------------------------------------------------------------}
destructor TMyObjectClass.Destroy;
begin
FMemo := nil;
FMemo.Free;
inherited Destroy;
end;
{''----------------------------------------------------------------------------}
{This function is executed everytime the Mean is retrived. That is, before the
Mean is returned, it is recalculated. But only if FNeedToCalc is true.}
function TMyObjectClass.GetMean: double;
begin
if FNeedToCalc then begin
{Typecast the memo lines as a TStringlist. This makes CalcMean somewhat more
generic.}
FMean := CalcMean(TStringList(FMemo.Lines));
FNeedToCalc := false;
end;
Result := FMean;
end;
{''----------------------------------------------------------------------------}
{Compute the mean of all the strings in a TStringList}
function TMyObjectClass.CalcMean
(
Data: TStringList {Each item in the list is considered a value}
): double; {Returns the mean of the above values}
var
i: integer;
Count: integer;
Sum: Double;
Mean: Double;
begin
Sum := 0;
Count := 0;
{Go through all the lines in the stringlist}
for i := 0 to Data.Count - 1 do begin
try
Sum := Sum + StrToFloat(Data[i]);
Inc(Count);
except
{If the line cannot be converted to a number, ignore it and go to the
next line.}
on EConvertError do begin
continue;
end;
end;
end;
if Count > 0 then begin
Mean := Sum / Count;
end else begin
Mean := 0;
end;
Result := Mean;
end;
end.